$(function(){
	$('#tabs').tabs();
	$('#newUserDialog').dialog({
		autoOpen: false,
		modal: true,
		buttons: {
			Create: function(){
				onClickAddStudentButton();
				$('#newUserDialog').dialog('close');	
			},
			Cancel: function(){
				$('#newUserDialog').dialog('close');
			}
		}
	});
	
	$("#newTADialog").dialog({
		autoOpen: false,
		modal: true,
		buttons: {
			Create: function(){
				onClickAddTAButton();
				$('#newTADialog').dialog('close');	
			},
			Cancel: function(){
				$('#newTADialog').dialog('close');
			}
		}
	});
	
	$('.newUser').click(function(){
		$('#newUserDialog').dialog('open');
	});
	
	$('.newTA').click(function(){
		$('#newTADialog').dialog('open');
	});
});

function onClickAddStudentButton() {
	var name = $("#add_student_name").val();
	var password = $("#add_student_password").val();
	$.post("teacher/addStudent", {
		name : name,
		password: password
	}, function(data){
		addRowToStudentTable(JSON.parse(data));
	});
}

function onClickAddTAButton() {
	var name = $("#add_ta_name").val();
	var password = $("#add_ta_password").val();
	$.post("teacher/addTA", {
		name : name,
		password: password
	}, function(data){
		var assistant = JSON.parse(data);
		addRowToTATable(assistant);
	});
}

function addRowToStudentTable(student) {
	var tr = $("<tr/>");
	var td1 = $("<td/>").text(student.id).appendTo(tr);
	var td2 = $("<td/>").text(student.name).appendTo(tr);
	$("#students_table").append(tr);
}

function addRowToTATable(assistant) {
	var tr = $("<tr/>");
	var td1 = $("<td/>").text(assistant.id).appendTo(tr);
	var td2 = $("<td/>").text(assistant.name).appendTo(tr);
	$("#tas_table").append(tr);
}


